package cn.lixufeng.user.service.impl;

import cn.lixufeng.basic.exception.BusinessException;
import cn.lixufeng.basic.service.impl.BaseServiceImpl;
import cn.lixufeng.basic.utils.MD5Utils;
import cn.lixufeng.basic.utils.StrUtils;
import cn.lixufeng.user.constant.UserConstants;
import cn.lixufeng.user.domain.LoginInfo;
import cn.lixufeng.user.domain.User;
import cn.lixufeng.user.dto.UserDto;
import cn.lixufeng.user.mapper.LoginInfoMapper;
import cn.lixufeng.user.mapper.UserMapper;
import cn.lixufeng.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    // 当修改User时应该级联修改LoginInfo
    @Override
    public void add(User user) {
        super.add(user);
    }

    @Override
    public void delete(Long id) {
        super.delete(id);
    }

    @Override
    public void update(User user) {
        super.update(user);
    }

    @Override
    public void register(UserDto userDto) {
        // 校验参数不能为空
        if (StringUtils.isEmpty(userDto.getPhone())
                || StringUtils.isEmpty(userDto.getVerifyCode())
                || StringUtils.isEmpty(userDto.getPassword())
                || StringUtils.isEmpty(userDto.getConfirmPassword())){
            throw new BusinessException("请输入完整的登录信息！");
        }

        // 校验该手机是否已注册
        User userTmp = userMapper.findByPhone(userDto.getPhone());
        if (userTmp != null){
            throw new BusinessException("该手机号码已被注册！");
        }

        // 校验两次输入的密码是否相同
        if (!userDto.getPassword().equals(userDto.getConfirmPassword())){
            throw new BusinessException("前后两次输入的密码不相同，请重新输入！");
        }

        // 校验验证码是否过期
        Object codeTmp = redisTemplate.opsForValue().get(UserConstants.BUSINESS_REGISTER_PREFIX + userDto.getPhone());
        if (codeTmp == null){
            throw new BusinessException("验证码已失效，请重新获取");
        }

        // 校验验证码是否正确
        String code = codeTmp.toString().split(":")[0];
        // 验证码一般忽略大小写equalsIgnoreCase
        if (!userDto.getVerifyCode().equalsIgnoreCase(code)){
            throw new BusinessException("验证码错误，请重新输入！");
        }

        // 保存数据
        User user = UserDto2User(userDto);
        LoginInfo loginInfo = user2LoginInfo(user);
        // 保存loginInfo
        loginInfoMapper.add(loginInfo);
        // 保存user
        user.setLogininfo_id(loginInfo.getId());
        userMapper.add(user);
    }

    /**
     * user存为LoginInfo
     * @param user
     * @return
     */
    private LoginInfo user2LoginInfo(User user) {
        LoginInfo loginInfo = new LoginInfo();
        // 属性同名原则拷贝
        BeanUtils.copyProperties(user,loginInfo);
        // type为用户
        loginInfo.setType(1);
        return loginInfo;
    }

    /**
     * UserDto数据传输对象存为User
     * @param userDto
     * @return
     */
    private User UserDto2User(UserDto userDto) {
        User user = new User();
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        // 盐值
        String salt = StrUtils.getComplexRandomString(32);
        // 用盐值作mds加密之后的密码 密码加盐
        String md5Pwd = MD5Utils.encrypByMd5(userDto.getPassword() + salt);// 加盐
        user.setSalt(salt);
        user.setPassword(md5Pwd);
        return user;
    }

}
